/**
 * 封装localStorage方法,添加ls前缀，防止ls的key值命名污染
 */
const prefix = 'TEST_'
let db = {
  save (key, value) {
    key=prefix+key
    localStorage.setItem(key, JSON.stringify(value))
  },
  get (key) {
    key=prefix+key
    return localStorage.getItem(key)? JSON.parse(localStorage.getItem(key)) : null
  },
  remove (key) {
    key=prefix+key
    localStorage.removeItem(key)
  },
  clear () {
    let len = localStorage.length
    let keys = []

    for (let i = 0; i < len; i++) {
      let key = localStorage.key(i)
      if (key && key.startsWith(prefix)) {
        keys.push(key)
      }
    }

    keys.map((key) => localStorage.removeItem(key))
  }
}

export default db
